#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <csignal>

using namespace std;

void handle_sigchld(int sig)
{
    int status;
    pid_t pid;
    while((pid = waitpid(-1, &status, WNOHANG)) > 0)
    {
        cout << "[Parent] Reaped child " << pid << endl;
    }
}

int main()
{
    signal(SIGCHLD, handle_sigchld);
    if(fork() == 0)
    {
        cout << "[Child] PID: " << getpid() << " exiting...\n";
        _exit(0);
    }
    cout << "[Parent] Waiting...\n";
    pause();
}
    

